import 'package:eh_mobile/common/utils/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:logger/logger.dart';

class BnwToast {
  static OverlayEntry? overlayEntry;

  static void showView(BuildContext context,
      {String? iconStr, String? infoStr, bool isSuccess = false}) {
    dismiss();
    overlayEntry = OverlayEntry(builder: (BuildContext context) {
      return ToastInfoView(
        overlayEntry: overlayEntry,
        iconStr: iconStr,
        infoStr: infoStr,
        isSuccess: isSuccess,
      );
    });
    Overlay.of(context).insert(overlayEntry!);
  }

  static void dismiss() {
    try {
      if (overlayEntry != null) {
        overlayEntry?.remove();
      }
    } catch (e) {
      Logger().d('异常$e');
    }
  }
}

class ToastInfoView extends StatefulWidget {
  OverlayEntry? overlayEntry;
  String? iconStr;
  String? infoStr;
  bool isSuccess = false;

  ToastInfoView(
      {this.overlayEntry,
      this.iconStr,
      this.infoStr,
      required this.isSuccess,
      super.key});

  @override
  State<ToastInfoView> createState() => _State();
}

class _State extends State<ToastInfoView> with SingleTickerProviderStateMixin {
  AnimationController? animationController;
  String? infoStr;
  bool isSuccess = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        widget.overlayEntry?.remove();
      },
      child: AnimatedBuilder(
          animation: animationController!,
        builder: (context, child) {
          return SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(0, 1), // 从底部滑入
              end: const Offset(0, 0.3),
            ).animate(CurvedAnimation(
              parent: animationController!,
              curve: Curves.easeInOut,
            ),),
            child: child,
          );
        },
        child: UnconstrainedBox(
          child: Container(
            height: 60.h,
            padding: EdgeInsets.only(left: 15.w, right: 20.w),
            decoration: BoxDecoration(
                color: const Color.fromRGBO(50, 50, 50, 1.0),
                borderRadius: BorderRadius.circular(15.r),
                border: Border.all(
                    color: const Color.fromRGBO(255, 255, 255, 0.08),
                    width: 1),
                boxShadow: const [
                  BoxShadow(
                    color: Color.fromRGBO(255, 255, 255, 0.05),
                    blurRadius: 50,
                    blurStyle: BlurStyle.outer,
                  ),
                ]),
            child: Row(
              children: [
                SvgPicture.asset(
                  (widget.iconStr == null || widget.isSuccess == false)
                      ? 'assets/svgs/set_up/ic_message_no.svg'
                      : 'assets/svgs/set_up/ic_message_ok.svg',
                ),
                18.sizedBoxWidth,
                Text(
                  widget.infoStr ?? '',
                  style: 16.ts,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
        vsync: this,
        duration: const Duration(milliseconds: 699),
        reverseDuration: const Duration(milliseconds: 499));
    animationController?.forward();
    animationController?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(const Duration(seconds: 2)).then((value) {
          if (animationController != null && mounted) {
            animationController?.reverse();
          }
        });
      } else if (status == AnimationStatus.dismissed) {
        if (widget.overlayEntry != null) {
          widget.overlayEntry?.remove();
        }
      }
    });
  }

  @override
  void dispose() {
    animationController?.dispose();
    super.dispose();
  }
}
